#!/bin/bash

#
# @date:2021-10-10
# @author:deep as the sea
# @orgnization:doitedu
# @desc:日活表计算脚本
#


export HIVE_HOME=/opt/apps/hive-3.1.2

dt=$(date -d'-1 day' +%Y-%m-%d)

if [ $1 ];then
echo "运行脚本，传入了指定的日期，将导指定日期的数据"
dt=$1
fi

dt_pre=$(date -d"${dt} -1day" +%Y-%m-%d)

master=local
if [ $2 ];then
  master=$2
  echo "任务运行模式： $master "
fi


${HIVE_HOME}/bin/hive -e "
SET mapreduce.framework.name=${master};
INSERT INTO TABLE dws.mall_app_dau PARTITION(dt='${dt}')
SELECT
  guid,
  count(1) as ses_cnt,
  sum(ses_pv_cnt) as pv_cnt,
  sum(ses_end_time-ses_start_time) as acc_time_amt
FROM dws.mall_app_ses_agr
WHERE dt='${dt}'
GROUP BY  guid
;


"


if [ $? -eq 0 ];then
  echo "任务成功：日活表计算成功"
  echo "目标表分区：dws.mall_app_dau  partition(dt='${dt}')"
  echo "任务成功：设日活表计算成功；目标表分区：dws.mall_app_dau partition(dt='${dt}')" | mail -s '多易集团,商城业务数据部，HIVE任务成功通知' 83544844@qq.com
  exit 0
else
  echo "任务失败：日活表计算失败"
  echo "目标表分区：dws.mall_app_dau  partition(dt='${dt}')"
  echo "任务失败：日活表计算失败；目标表分区：dws.mall_app_dau  partition(dt='${dt}')" | mail -s '多易集团,商城业务数据部，HIVE任务失败通知' 83544844@qq.com
  exit 0
fi
